package com.star.starp.utils;

import java.lang.reflect.Field;

/**
 * @author alex
 * @date 2022/1/2 17:04
 * @description:TODO
 */
public class BeanUtils {

    public static <T> T copyBean(Class<T> clz, Object obj, Field[] fields) {
        T t = null;
        try {
            //实例化对象
            t = clz.newInstance();
            //遍历属性
            for (Field f : fields) {
                Field field = clz.getDeclaredField(f.getName());
                if (field != null) {
                    field.setAccessible(true);
                    f.setAccessible(true);
                    field.set(t,f.get(obj));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
}
